﻿using System;
using NUnit.Framework;
using Present.Commands.Async.Actions;
using Present.Commands.Async.Actions.Decorators;
using Rhino.Mocks;

namespace Present.Commands.Tests.Decorators
{
    [TestFixture]
    public class ObservableAsyncActionDecoratorTestCase : AsyncActionDecoratorTestCaseBase<ObservableAsyncActionDecorator<int, int>, int, int>
    {
      
        [Test]
        public void DoWorkCalledOnInnerTarget()
        {

            Decorator.DoWork(Decorator.PrepareWork(1));
            Inner.AssertWasCalled(x => x.DoWork(Decorator.PrepareWork(1)));
        }



        [Test]
        public void OnErrorCalledOnError()
        {
            Exception thrown = null;
            Decorator.Subscribe(x => { }, e => thrown = e);

            var exception = new Exception();
            Decorator.HandleError(exception);
            Assert.AreSame(exception, thrown);
        }

        [Test]
        public void OnNextCalledOnSuccess()
        {
            int result = 0;
            Decorator.Subscribe(x => result = (int)x);

            Decorator.HandleSuccess(1);
            Assert.AreEqual(1, result);
        }

        [Test(Description = "This is needed ATM for the composite target which uses forkjoin, may need to look into this later")]
        public void OnCompletedCalledOnSuccess()
        {
            var completed = false;
            Decorator.Subscribe(x => { }, () => { completed = true; });

            Decorator.HandleSuccess(1);
            Assert.IsTrue(completed);
        }

        protected override ObservableAsyncActionDecorator<int, int> CreateDecorator()
        {
            return Inner.ToObservable();
        }
        
        protected override int GetParameter()
        {
            return 1;
        }

        protected override int GetResult()
        {
            return 1;
        }
    }
}
